package com.sudo.controller;

import com.sudo.aspect.AuthorizationApi;
import com.sudo.common.enums.MenuParams;
import com.sudo.common.enums.SysStatus;
import com.sudo.common.utils.JsonRes;
import com.sudo.pojo.SysButton;
import com.sudo.pojo.SysMenu;
import com.sudo.pojo.SysRoleButton;
import com.sudo.pojo.bo.SysButtonBO;
import com.sudo.pojo.vo.SimpleSysMenuVO;
import com.sudo.service.SysButtonService;
import com.sudo.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author：为道日损 2020-04-28 8:42
 */
@Api(value = "按钮接口",tags = {"按钮相关接口"})
@RestController
@RequestMapping("sysButton")
public class SysButtonController extends BaseController {

    @Autowired
    private SysButtonService sysButtonService;

    @Autowired
    private SysMenuService sysMenuService;

    @AuthorizationApi(menu = "btnConf",button = "search")
    @ApiOperation(value = "根据菜单代码查找按钮",notes = "根据菜单代码查找按钮",httpMethod = "GET")
    @GetMapping("/getSysButtonByMenuCode")
    public JsonRes getSysButtonByMenuCode(@RequestParam String menuCode) {

        List<SysButton> sysButtonList = sysButtonService.getSysButtonByMenuCode(menuCode);

        return JsonRes.ok(sysButtonList);
    }

    @AuthorizationApi(menu = "btnConf",button = "search")
    @ApiOperation(value = "根据菜单代码查找简单版按钮",notes = "根据菜单代码查找简单版按钮",httpMethod = "GET")
    @GetMapping("/getSimpleButtonByMenuCode")
    public JsonRes getSimpleButtonByMenuCode(@RequestParam String menuCode) {

        List<SysButton> sysButtonList = sysButtonService.getSimpleButtonByMenuCode(menuCode);

        return JsonRes.ok(sysButtonList);
    }

    @AuthorizationApi(menu = "btnConf",button = "search")
    @ApiOperation(value = "根据buttonCode获取按钮",notes = "根据buttonCode获取按钮",httpMethod = "GET")
    @GetMapping("/getSysButtonByMenuButtonCode")
    public JsonRes getSysButtonByMenuButtonCode(@RequestParam("menuCode") String menuCode,
                                      @RequestParam("buttonCode") String buttonCode) {

        SysButton sysButton = sysButtonService.getSysButtonByMenuButtonCode(menuCode,buttonCode);
        return JsonRes.ok(sysButton);
    }

    @AuthorizationApi(menu = "btnConf",button = "update")
    @ApiOperation(value = "根据menuButtonCode修改按钮",notes = "根据menuButtonCode修改按钮",httpMethod = "POST")
    @PostMapping("/updateSysButtonByMenuButtonCode")
    public JsonRes updateSysButtonByMenuButtonCode(@RequestBody @Valid SysButtonBO sysButtonBO,
                                       BindingResult result,
                                       HttpServletRequest request) {
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("仅有超级用户能执行该操作");
        }
        if (superMenu(sysButtonBO.getMenuCode())) {
            return JsonRes.errorMsg("系统固有菜单下的按钮禁止修改");
        }
        int affectCount = sysButtonService.updateSysButtonByMenuButtonCode(sysButtonBO,sessionUser);

        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "btnConf",button = "add")
    @ApiOperation(value = "新增按钮",notes = "新增按钮",httpMethod = "POST")
    @PostMapping("/addSysButton")
    public JsonRes addSysButton(@RequestBody @Valid SysButtonBO sysButtonBO,
                                       BindingResult result,
                                       HttpServletRequest request) {
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("仅有超级用户能执行该操作");
        }
        if (superMenu(sysButtonBO.getMenuCode())) {
            return JsonRes.errorMsg("禁止给固有菜单添加按钮");
        }
        //判断菜单是否存在
        SysMenu sysMenu = sysMenuService.getSysMenuByCode(sysButtonBO.getMenuCode());
        if (StringUtils.isEmpty(sysMenu)
                || SysStatus.OFF.value.equals(sysMenu.getStatus())) {
            return JsonRes.errorMsg("当前菜单不可用");
        }

        //判断按钮code是否已存在
        boolean flag = sysButtonService.isButtonCodeExist(sysButtonBO.getMenuCode(),sysButtonBO.getbCode());
        if (flag) {
            return JsonRes.errorMsg("代码已存在，请重新编辑");
        }

        int affectCount = sysButtonService.addSysButton(sysButtonBO,sessionUser);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "btnConf",button = "delete")
    @ApiOperation(value = "删除按钮",notes = "删除按钮",httpMethod = "POST")
    @PostMapping("/deleteSysButtonByMenuButtonCode")
    public JsonRes deleteSysButtonByMenuButtonCode(
            @RequestParam("menuCode") String menuCode,
            @RequestParam("buttonCode") String buttonCode,
            HttpServletRequest request) {
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("仅有超级用户能执行该操作");
        }
        if (superMenu(menuCode)) {
            return JsonRes.errorMsg("系统固有菜单下的按钮禁止删除");
        }
        int affectCount = sysButtonService.deleteSysButtonByMenuButtonCode(menuCode,buttonCode);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "btnConf",button = "search")
    @ApiOperation(value = "获取简单版-级联菜单",notes = "获取简单版级联菜单",httpMethod = "GET")
    @GetMapping("/getSimpleCascadeSysMenuList")
    public JsonRes getSimpleCascadeSysMenuList(@RequestParam("status") String status) {

        List<SimpleSysMenuVO> simpleSysMenuVOList = sysMenuService.getSimpleCascadeSysMenuList(MenuParams.MENU_ROOT_PCODE.value,status);

        return JsonRes.ok(simpleSysMenuVOList);
    }


    @AuthorizationApi(menu = "btnConf",button = "search")
    @ApiOperation(value = "根据roleCode获取对应菜单按钮权限",notes = "根据roleCode获取对应菜单按钮权限",httpMethod = "GET")
    @GetMapping("getButtonCodeByRoleMenuCode")
    public JsonRes getButtonCodeByRoleMenuCode(@RequestParam String roleCode,
                                               @RequestParam String menuCode) {

        List<SysRoleButton> sysRoleButtonList = sysButtonService.getButtonCodeByRoleMenuCode(roleCode,menuCode);

        return JsonRes.ok(sysRoleButtonList);
    }
}
